//
//  UIButton+AFFApp.m
//  AnyfishApp
//
//  Created by apple on 14-3-25.
//  Copyright (c) 2014年 Anyfish. All rights reserved.
//

#import "UIButton+AFF.h"

@implementation UIButton (AFF)

static void * codeBgKey = (void *)@"ButtonBgTextKey";
static void * codeBtnImageSourceKey = (void *)@"ButtonImageSourceKey";

- (void)setImageNameSource:(NSString *)imageNameSource {
    objc_setAssociatedObject(self, codeBtnImageSourceKey, imageNameSource, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString*)imageNameSource {
    return objc_getAssociatedObject(self, codeBtnImageSourceKey);
}

#pragma mark 扩展方法

- (void)setImageWithFileNameDownload:(NSString *)name isFull:(BOOL)isFull isBg:(BOOL)isBg {
    if([NSString isNilOrEmpty:name])
        return;
    
    self.imageNameSource = name;
    
    NSString *fileName = isFull ? [UIImage imageNameFileNameDownloadFullScreen:name] : [UIImage imageNameFileNameDownload:name];
    
    
    UIImage *img = [UIImage imageWithContentsOfFile:fileName];
    if(img){
        if(isBg)
            [self setBackgroundImage:img forState:UIControlStateNormal];
        else
            [self setImage:img forState:UIControlStateNormal];
    }
}

- (void)customStyle:(CGFloat)r green:(CGFloat)g blue:(CGFloat)b alpha:(CGFloat)a cornerRadius:(CGFloat)radius {
    UIColor *temp = [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a];
    [self customStyleWithColor:temp cornerRadius:radius];
}

- (void)customStyleWithColor:(UIColor*)color  cornerRadius:(CGFloat)radius {
    [self customStyleWithColor:color cornerRadius:radius borderWidth:0 borderColor:nil];
}

- (void)customStyle:(NSString*)argb cornerRadius:(CGFloat)radius {
    [self customStyle:argb cornerRadius:radius borderWidth:0 borderColorStr:nil];
}

- (void)customStyleWithColor:(UIColor*)color  cornerRadius:(CGFloat)radius borderWidth:(CGFloat)borderwidth borderColor:(UIColor*)borderColor {
    if(![color isEqual:[UIColor clearColor]]){
        NSArray *colors = [UIColor colorsWithARGBString:[UIColor colorToString:color]];
        UIColor *colorx = [colors objectWithIndex:0];
        [self setBackgroundImage:[self buttonImageFromColor:colorx] forState:UIControlStateNormal];
        
        colorx = [colors objectWithIndex:1];
        [self setBackgroundImage:[self buttonImageFromColor:colorx] forState:UIControlStateHighlighted];
    }else{
        [self setBackgroundImage:nil forState:UIControlStateNormal];
    }
    
    if(borderwidth>0){
        self.layer.borderWidth = borderwidth;
        if(borderColor)
            self.layer.borderColor = [borderColor CGColor];
    }
    
    self.layer.cornerRadius = radius;
    self.layer.masksToBounds = YES;
    [self setAdjustsImageWhenHighlighted:NO];
}

- (void)customStyle:(NSString*)argb cornerRadius:(CGFloat)radius borderWidth:(CGFloat)borderwidth borderColorStr:(NSString*)borderColorStr{
    [self customStyleWithColor:[UIColor colorWithARGBString:argb] cornerRadius:radius borderWidth:borderwidth borderColor:[UIColor colorWithARGBString:borderColorStr]];
}

- (void)addAwesomeIcon:(FAIcon)icon beforeTitle:(BOOL)before {
    NSString *iconString = [NSString stringFromAwesomeIcon:icon];
    self.titleLabel.font = [UIFont fontWithName:@"FontAwesome"
                                           size:self.titleLabel.font.pointSize];
    
    NSString *title = [NSString stringWithFormat:@"%@", iconString];
    
    if(self.titleLabel.text) {
        if(before)
            title = [title stringByAppendingFormat:@" %@", self.titleLabel.text];
        else
            title = [NSString stringWithFormat:@"%@  %@", self.titleLabel.text, iconString];
    }
    
    [self setTitle:title forState:UIControlStateNormal];
}

- (UIImage *)buttonImageFromColor:(UIColor *)color {
    CGRect rect = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    return [UIImage imageWithColor:color frame:rect];
}

- (void)layoutButtonWithEdgeInsetsStyle:(ButtonEdgeInsetsStyle)style imageTitlespace:(CGFloat)space {
    CGFloat imageViewWidth = CGRectGetWidth(self.imageView.frame);
    CGFloat labelWidth = CGRectGetWidth(self.titleLabel.frame);
    
    if (labelWidth == 0) {
        CGSize titleSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
        labelWidth  = titleSize.width;
    }
    
    CGFloat imageInsetsTop = 0.0f;
    CGFloat imageInsetsLeft = 0.0f;
    CGFloat imageInsetsBottom = 0.0f;
    CGFloat imageInsetsRight = 0.0f;
    
    CGFloat titleInsetsTop = 0.0f;
    CGFloat titleInsetsLeft = 0.0f;
    CGFloat titleInsetsBottom = 0.0f;
    CGFloat titleInsetsRight = 0.0f;
    
    switch (style) {
        case ButtonEdgeInsetsStyleImageRight:
        {
            space = space * 0.5;
            
            imageInsetsLeft = labelWidth + space;
            imageInsetsRight = -imageInsetsLeft;
            
            titleInsetsLeft = - (imageViewWidth + space);
            titleInsetsRight = -titleInsetsLeft;
        }
            break;
            
        case ButtonEdgeInsetsStyleImageLeft:
        {
            space = space * 0.5;
            
            imageInsetsLeft = -space;
            imageInsetsRight = -imageInsetsLeft;
            
            titleInsetsLeft = space;
            titleInsetsRight = -titleInsetsLeft;
        }
            break;
        case ButtonEdgeInsetsStyleImageBottom:
        {
            CGFloat imageHeight = CGRectGetHeight(self.imageView.frame);
            CGFloat labelHeight = CGRectGetHeight(self.titleLabel.frame);
            CGFloat buttonHeight = CGRectGetHeight(self.frame);
            CGFloat boundsCentery = (imageHeight + space + labelHeight) * 0.5;
            
            CGFloat centerX_button = CGRectGetMidX(self.bounds); // bounds
            CGFloat centerX_titleLabel = CGRectGetMidX(self.titleLabel.frame);
            CGFloat centerX_image = CGRectGetMidX(self.imageView.frame);
            
            CGFloat imageBottomY = CGRectGetMaxY(self.imageView.frame);
            CGFloat titleTopY = CGRectGetMinY(self.titleLabel.frame);
            
            imageInsetsTop = buttonHeight - (buttonHeight * 0.5 - boundsCentery) - imageBottomY;
            imageInsetsLeft = centerX_button - centerX_image;
            imageInsetsRight = - imageInsetsLeft;
            imageInsetsBottom = - imageInsetsTop;
            
            titleInsetsTop = (buttonHeight * 0.5 - boundsCentery) - titleTopY;
            titleInsetsLeft = -(centerX_titleLabel - centerX_button);
            titleInsetsRight = - titleInsetsLeft;
            titleInsetsBottom = - titleInsetsTop;
            
        }
            break;
        case ButtonEdgeInsetsStyleImageTop:
        {
            CGFloat imageHeight = CGRectGetHeight(self.imageView.frame);
            CGFloat labelHeight = CGRectGetHeight(self.titleLabel.frame);
            CGFloat buttonHeight = CGRectGetHeight(self.frame);
            CGFloat boundsCentery = (imageHeight + space + labelHeight) * 0.5;
            
            CGFloat centerX_button = CGRectGetMidX(self.bounds); // bounds
            CGFloat centerX_titleLabel = CGRectGetMidX(self.titleLabel.frame);
            CGFloat centerX_image = CGRectGetMidX(self.imageView.frame);
            
            CGFloat imageTopY = CGRectGetMinY(self.imageView.frame);
            CGFloat titleBottom = CGRectGetMaxY(self.titleLabel.frame);
            
            imageInsetsTop = (buttonHeight * 0.5 - boundsCentery) - imageTopY;
            imageInsetsLeft = centerX_button - centerX_image;
            imageInsetsRight = - imageInsetsLeft;
            imageInsetsBottom = - imageInsetsTop;
            
            titleInsetsTop = buttonHeight - (buttonHeight * 0.5 - boundsCentery) - titleBottom;
            titleInsetsLeft = -(centerX_titleLabel - centerX_button);
            titleInsetsRight = - titleInsetsLeft;
            titleInsetsBottom = - titleInsetsTop;
        }
            break;
            
        default:
            break;
    }
    
    self.imageEdgeInsets = UIEdgeInsetsMake(imageInsetsTop, imageInsetsLeft, imageInsetsBottom, imageInsetsRight);
    self.titleEdgeInsets = UIEdgeInsetsMake(titleInsetsTop, titleInsetsLeft, titleInsetsBottom, titleInsetsRight);
}

@end
